package com.easyexcel.demo.component;

import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.easyexcel.demo.converter.AdaptiveWidthStyleStrategy;
import com.easyexcel.demo.converter.CustomStyleStrategy;
import com.easyexcel.demo.service.UserService;
import com.easyexcel.demo.util.EasyExcelUtil;
import com.easyexcel.demo.vo.UserQuery;
import com.easyexcel.demo.vo.UserVO;
import com.google.common.collect.Lists;
import lombok.SneakyThrows;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author zhaoxh
 * @version : UserNoModelWrite.java, v 0.1 2023/12/22 zhaoxh Exp $
 * @desc  又来了个自定义动态导出需求
 */
@Component
public class UserNoModelService {

    @Resource
    private UserService userService;

    private static final String EXPORT_FILE_NAME_TEMPLATE = "【%s】用户信息导出";


    @SneakyThrows
    public void exportExcel(HttpServletRequest request, HttpServletResponse response, UserQuery query) {
        query.setCurrent(1);
        query.setSize(Integer.MAX_VALUE);

        // 文件名
        String fileName = String.format(EXPORT_FILE_NAME_TEMPLATE, DateUtil.now().replace(":", "_"));

        // 获取流
        ServletOutputStream outputStream = response.getOutputStream();

        // 设置响应
        EasyExcelUtil.setResponseHeaders(response, fileName);

        // 创建 ExcelWriter
        ExcelWriter excelWriter = EasyExcel.write(outputStream).head(head())
                .registerWriteHandler(new CustomStyleStrategy())
                .registerWriteHandler(new AdaptiveWidthStyleStrategy())
                .build();


        for (int i = 0; i < 2; i++) {
            // 创建 Sheet
            WriteSheet writeSheet = EasyExcelFactory.writerSheet("用户信息" + i).build();
            // 写入数据
            excelWriter.write(dataUserList(query), writeSheet);
        }

        // 关闭资源
        excelWriter.finish();

        // 刷新并关闭输出流
        outputStream.flush();
        outputStream.close();
    }


    /**
     * 可以根据数据动态的去渲染表头
     *
     * @return
     */
    private List<List<String>> head(){
        List<List<String>> list = Lists.newArrayList();
        List<String> head0 = Lists.newArrayList();
        head0.add("主键id");
        List<String> head1 = Lists.newArrayList();
        head1.add("姓名");
        List<String> head2 = Lists.newArrayList();
        head2.add("性别");
        List<String> head3 = Lists.newArrayList();
        head3.add("年龄");
        List<String> head4 = Lists.newArrayList();
        head4.add("生日");
        List<String> head5 = Lists.newArrayList();
        head5.add("创建时间");
        List<String> head6 = Lists.newArrayList();
        head6.add("薪水");
        list.add(head0);
        list.add(head1);
        list.add(head2);
        list.add(head3);
        list.add(head4);
        list.add(head5);
        list.add(head6);
        return list;
    }


    /**
     *  可以根据数据动态的去渲染数据
     *
     * @return
     */
    private List<List<Object>> dataUserList(UserQuery query) {
        List<UserVO> records = userService.page(query).getRecords();
        List<List<Object>> objectList = new ArrayList<>();
        for (UserVO userVO : records) {
            List<Object> row = new ArrayList<>();
            row.add(toEmptyString(userVO.getId()));
            row.add(toEmptyString(userVO.getName()));
            row.add(toEmptyString(userVO.getSex()));
            row.add(toEmptyString(userVO.getAge()));
            row.add(toEmptyString(userVO.getBirthday()));
            row.add(toEmptyString(userVO.getCreateTime()));
            row.add(toEmptyString(userVO.getSalary()));
            objectList.add(row);
        }
        return objectList;
    }

    public  String toEmptyString(Object value) {
        return Objects.nonNull(value) ? value.toString() : "";
    }

}
